# coding: UTF-8 -*-

import datetime
from relativedelta import relativedelta
from GMT.messaging.xmlpickle import PickleMeToXML

RequestTimeSpan = ("WEEK", "4WEEKS", "EVER",) 
class Request(PickleMeToXML):
    time_span = "WEEK"
    
    def __init__(self, username, password, generateGuid=None, async=False):
        self.username = username
        self.password = password
        self.async = async
        if generateGuid is None:
            self.id = None
        else:
            self.id = guid()
            
    def get_timespan_date(self):
        return _time_span_to_date(self.time_span)
            
    
            
ResponseCodes = ("OK", "INPUT_ERROR", "INTERNAL_SERVER_ERROR", "IMAP_LOGIN_ERROR", "SERVER_UNKNOWN_ERROR",)
class Response(PickleMeToXML):
    __pickle_as_cdata__ = ["data"]
    time_span = "WEEK"
    
    def __init__(self, id, code, data=None):
        PickleMeToXML.__init__(self)
        self.id = id
        if (code.upper() in ResponseCodes):
            self.code = code.upper()
        else:
            self.code = None
        self.data = data
        
    def get_timespan_date(self):
        return _time_span_to_date(self.time_span)

def _time_span_to_date(time_span):
    if not time_span in RequestTimeSpan:
        time_span = "WEEK"
    
    if time_span == "WEEK":
        return datetime.date.today()+relativedelta(weeks=-1)
    elif time_span == "4WEEKS":
        return datetime.date.today()+relativedelta(weeks=-4)
    elif time_span == "EVER":
        return None
    else:
        return datetime.date.today()+relativedelta(weeks=-1)   

class RootObject(PickleMeToXML):

    counter = 3
    def __init__(self):
        self.sub = SubObject()
        self.data = dict(xyz=4711)
        self.objlist = [ 1, 2, SubObject(), DetailObject() ]

class SubObject(PickleMeToXML):

    __pickle_to_xml__ = ["values", "detail"]

    def __init__(self):
        self.values = (3.12, 4711, 8.15)
        self.z = "uwe"
        self.detail = DetailObject()

class DetailObject(PickleMeToXML):

    statement = "1 < 2 is true"
    blablaliste = [u"שלום", "b", "c"]

    def _xml_encode_statement(self):
        # encrypt attribute 'statement'
        return self.statement[::-1]

    def _xml_decode_statement(self, value):
        # decrypt value
        self.statement = value[::-1]
        
import time
import random
import socket
import md5
        
def guid( *args ):
    """
    Generates a universally unique ID.
    Any arguments only create more randomness.
    """
    t = long( time.time() * 1000 )
    r = long( random.random()*100000000000000000L )
    try:
        a = socket.gethostbyname( socket.gethostname() )
    except:
        # if we can't get a network address, just imagine one
        a = random.random()*100000000000000000L
    data = str(t)+' '+str(r)+' '+str(a)+' '+str(args)
    m = md5.new()
    m.update(data)
    data = m.hexdigest()

    return data